<?php

class Default_Form_FoodIntake extends Zend_Form
{
    
    const DEFAULT_PRODUCTS_NUMBER = 1;
    
    private $_products_number = self::DEFAULT_PRODUCTS_NUMBER;
    
     /**
     * Constructor
     *
     * Registers form view helper as decorator
     *
     * @param mixed $options
     * @return void
     */
    public function __construct($options = null)
    {
        $products_number = (int)$options;
        
        if ($products_number > 0) {
            $this->_products_number = $products_number;
        }
        
        parent::__construct($options);
    }
    
    /**
     * Initialize form (used by extending classes)
     *
     * @return void
     */
    public function init() 
    {
        /*$element = new Zend_Form_Element_Text('date');
        $element
            ->setLabel('Дата')
            ->setRequired()
            ->addValidator('Date', false, array('DD-MM-YYYY'))
            ;
        $this->addElement($element);*/
        
        /*$options = $this->_getFoodIntakesOptions();
        $element = new Zend_Form_Element_Select('food_intake_code');
        $element
            ->setLabel('Прием пищи')
            ->setRequired()
            ->addMultiOptions($options)
            ->setValue(reset($options))
            ->addValidator('InArray', false, array(array_keys($options)))
            ;
        $this->addElement($element);*/
        
        $element = new Zend_Form_Element_Hidden('date');
        $this->addElement($element);
        
        $element = new Zend_Form_Element_Hidden('food_intake_code');
        $this->addElement($element);
        
        require_once APPLICATION_PATH . '/modules/products/filters/Double.php';
        $filter = new Products_Filter_Double;
        require_once APPLICATION_PATH . '/modules/products/validators/ProductExists.php';
        $validator = new Products_Validator_ProductExists;
        
        $i = 1;
        do {
            $element = new Zend_Form_Element_Text('product_' . $i);
            $element
                ->setRequired()
                ->addFilter('StringTrim')
                ->addValidator($validator)
                ->setAttrib('class', 'tag_add_input')
                ;
            $this->addElement($element);
            
            $element = new Zend_Form_Element_Text('gramme_' . $i);
            $element
                ->setRequired()
                ->addFilter('StringTrim')
                ->addFilter($filter)
                ->addValidator('GreaterThan', false, array(0))
                ->setAttrib('class', 'gramme_input')
                ;
            $this->addElement($element);
            
            $i++;
        } while($i <= $this->_products_number);
        
        $element = new Zend_Form_Element_Submit('submit');
        $element
            ->setLabel('Сохранить')
            ->setIgnore(true)
            ;
        $this->addElement($element);
    }
    
    private function _getFoodIntakesOptions() 
    {
        $model = new FoodIntake;
        return $model->getOptions();
    }
    
    /**
     * Retrieve all form element values
     *
     * @param  bool $suppressArrayNotation
     * @return array
     */
    public function getValues($suppressArrayNotation = false)
    {
        $data = array();
        $values = parent::getValues($suppressArrayNotation);
        
        foreach ($values as $key => $value) {
            if (in_array($key, array('date', 'food_intake_code'))) {
                $data[$key] = $value;
            } else {
                if (!array_key_exists('products', $data)) {
                    $data['products'] = array();
                }
                
                $index = (int)substr($key, strpos($key, '_') + 1);
                if (!array_key_exists($index, $data['products'])) {
                    $data['products'][$index] = array();
                }
                
                $data['products'][$index] = array_merge
                (
                    $data['products'][$index], 
                    array(substr($key, 0, strpos($key, '_')) => $value)
                );
            }
        }
        
        return $data;
    }
    
    public function setValues(Doctrine_Collection $collection, $date, $food_intake) 
    {
        $data = array
        (
            'date' => $date, 
            'food_intake_code' => $food_intake, 
        );
        
        for($i = 0, $index = 1; $i < $collection->count(); $i++, $index++) {
            $food_intake = $collection[$i];
            $data['product_' . $index] = $food_intake->Products->name;
            $data['gramme_' . $index] = $food_intake->gramme;
        }
        
        $this->populate($data);
    }
    
}